<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
  import echarts from 'echarts'
  require('echarts/theme/macarons') // echarts theme
  import resize from './mixins/resize'
  import { getHotCount } from "../../api/hotsearch/index";

  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '350px'
      },
      autoResize: {
        type: Boolean,
        default: true
      }
    },
    data() {
      return {
        chart: null
      }
    },
    mounted() {
      // this.$nextTick(() => {
      //   this.initChart()
      // })
      let hotNum = {num : 20}
      getHotCount(hotNum).then(response => {
        if(response.data.length > 0){
          let data = response.data;
          let nameAry = [];
          let valueAry = [];
          for (let i in data) {
            let v = data[i];
            nameAry.push(v.hotword);
            valueAry.push(v.num);
          }
          let dataHotWord = [nameAry, valueAry];
          this.initChart(dataHotWord);
        }
      });
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart(dataHotWord) {
        this.chart = echarts.init(this.$el, 'macarons')
        this.chart.setOption({
          xAxis: {
            data: dataHotWord[0],
            boundaryGap: false,
            axisTick: {
              show: false
            },
            show:true
          },
          grid: {
            left: 10,
            right: 10,
            bottom: 20,
            top: 30,
            containLabel: true
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross'
            },
            padding: [5, 10]
          },
          yAxis: {
            axisTick: {
              show: false
            },
            splitLine: {
              show: false
            },
            show:false,
          },
          series: [
            {
              name: '热搜条目',
              smooth: true,
              type: 'line',
              itemStyle: {
                normal: {
                  color: '#3888fa',
                  label:{
                    show: true
                  },
                  lineStyle: {
                    color: '#3888fa',
                    width: 2
                  },
                  areaStyle: {
                    color: '#f3f8ff'
                  }
                }
              },
              data: dataHotWord[1],
              animationDuration: 2800,
              animationEasing: 'quadraticOut',
              areaStyle:{
                normal:{
                  //右，下，左，上
                  color:new echarts.graphic.LinearGradient(0,0,0,1,[
                    {
                      offset:0,
                      color:'#3888fa'
                    },
                    {
                      offset:1,
                      color:'#ffffff'
                    }
                  ],false)

                }
              }
            }]
        })
      }
    }
  }
</script>
